/**
*add by wuyx 销售品明细回显
*/
const pageOfferDetail = {


    /**
     * 订单明细回显主函数
     * @param {*} data 实例数据
     * @param {*} objArr formData 数据项
     * @param {*} mainProdId 主产品ID
     * @returns 
     */
    RewObject: (data, objArr,temp) => {//明细接口回显的数据，formdata,productId
       
       var orderTaskId = "";
        let orderDetails = data.ORDER_DETAIL;
        for (let key in objArr) {
            let item = objArr[key];
            for (let i = 0; i < orderDetails.length; i++) {
                if (item.tableName == 'CUSTOMER_ORDER') {
                    orderDetails[i][item.tableName].forEach(m => {
                        if(item.colId=="handleType"){
                            item.value=""+pageOfferDetail.getVal(m[item.colId]);
                            item.oldValue=""+pageOfferDetail.getVal(m[item.colId]);
                        }else {
                            item.value=pageOfferDetail.getVal(m[item.colId]);
                            item.oldValue= pageOfferDetail.getVal(m[item.colId]);
                        }

                        item.instId=m['custOrderId'];
                    });
                }
                else if (item.tableName == 'ORD_CUSTOMER') {
                    orderDetails[i][item.tableName].forEach(m => {
                        item.value= pageOfferDetail.getVal(m[item.colId]);
                        item.oldValue=pageOfferDetail.getVal(m[item.colId]);
                    });
                }
                else if (item.tableName == 'CUSTOMER') {
                    orderDetails[i][item.tableName].forEach(m => {
                            item.value= pageOfferDetail.getVal(m[item.colId]);
                            item.oldValue=pageOfferDetail.getVal(m[item.colId]);                  

                    });
                }
                else if (item.tableName == 'ORDER_ITEM') {
                    let orderItem=pageOfferDetail.getOrderItemByMainProdId(orderDetails[i][item.tableName],mainProdId)
                    // 
                    if(orderItem!=undefined&&orderItem!=null){
                        item.value = pageOfferDetail.getVal(orderItem[item.colId]);
                        item.oldValue=pageOfferDetail.getVal(orderItem[item.colId]);
                        item.instId=orderItem['orderItemId'];
                    }
                }
                else if (item.tableName == 'ORDER_ITEM_ATTR') {
                    orderDetails[i][item.tableName].forEach(m => {
                        if (m.attrId == item.colId) {
                            item.value = m.attrValue;
                            item.oldValue=m.attrValue;
                            item.instId=m.orderItemAttrId;
                        }
                    });
                }
                else if (item.tableName == 'ORD_PROD_INST') {
                    orderDetails[i][item.tableName].forEach(m => {
                        if (m.roleCd == item.attrRoleCd) {
                            if(item.colId=='lanId'){
                                item.value =pageOfferDetail.getVal(m['locName']); 
                                item.oldValue= pageOfferDetail.getVal(m['locName']);
                            }else{
                                item.value =pageOfferDetail.getVal(m[item.colId]);
                                item.oldValue=pageOfferDetail.getVal(m[item.colId]);
                            }
                            if(item.colId=='useCustId'){
                                item.value=pageOfferDetail.getVal(m[item.colId]);
                                item.text=pageOfferDetail.getVal(m['useCustName']);
                            }
                            item.instId=m['prodInstId'];  
                            item.rowId=m['rowId'];
                                                                                
                        }
                        if (m.roleCd == 0 && !item.attrRoleCd) {
                            if(item.colId=='lanId'){
                                item.value =pageOfferDetail.getVal(m['locName']); 
                                item.oldValue= pageOfferDetail.getVal(m['locName']);
                            } else if(item.colId=='payObjId'&&m['payObjName']!=undefined){
                                item.value =pageOfferDetail.getVal(m['payObjName']); 
                                item.oldValue= pageOfferDetail.getVal(m['payObjName']);
                            }else if(item.id=='141000400'){
                                let pordInst100=pageOfferDetail.getDataByRoleCd(orderDetails[i]['ORD_PROD_INST'],100);
                                item.value =pageOfferDetail.getVal(pordInst100['prodInstId']);
                                item.oldValue=pageOfferDetail.getVal(pordInst100['prodInstId']);
                            }else if(item.id=='141000401'){
                                let pordInst200=pageOfferDetail.getDataByRoleCd(orderDetails[i]['ORD_PROD_INST'],200);
                                item.value =pageOfferDetail.getVal(pordInst200['prodInstId']);
                                item.oldValue=pageOfferDetail.getVal(pordInst200['prodInstId']);
                            }else{
                                item.value = pageOfferDetail.getVal(m[item.colId]);  
                                item.oldValue= pageOfferDetail.getVal(m[item.colId]);  
                            }
                            item.instId=m['prodInstId'];   
                            item.rowId=m['rowId'];
                        } 
                                       
                    });

                }
                else if (item.tableName == 'ORD_PROD_INST_ATTR') {
                    orderDetails[i][item.tableName].forEach(m => {
                        if (m.roleCd == item.attrRoleCd&&m.attrId==item.colId) {
                            if(m.attrId==100010765){
                                item.value =parseInt(m.attrValue);
                                item.oldValue=parseInt(m.attrValue);
                            }else{
                                item.value =m.attrValue;  
                                item.oldValue=m.attrValue; 
                            }   
                            item.instId=m['prodInstAttrId'];  
                            item.rowId=m['rowId'];                                                                       
                        }
                        if (m.roleCd == 0 && !item.attrRoleCd&&m.attrId==item.colId) {
                            if(m.attrId==100010765){
                                item.value =parseInt(m.attrValue)
                                item.oldValue=parseInt(m.attrValue);
                            }else if(item.text!=undefined){
                                item.value =m.attrValue;  
                                item.oldValue=m.attrValue; 
                                item.text = m['attrValueName'];
                            } else{
                                item.value =m.attrValue; 
                                item.oldValue=m.attrValue; 
                            }  
                            item.instId=m['prodInstAttrId'];     
                            item.rowId=m['rowId'];
                        }  
                                       
                    });
                }
                else if (item.tableName == 'ORDER_TASK') {

                    // 
                    var newList = orderDetails[i][item.tableName].filter((o) =>{return o.taskId == temp.taskId})
                    
                    newList.forEach(m => {
                        item.value= pageOfferDetail.getVal(m[item.colId]);
                        item.oldValue=pageOfferDetail.getVal(m[item.colId]);   
                        item.instId=m['orderTaskId'];
                        
                    });
                }
                else if (item.tableName == 'ORDER_TASK_ATTR'&&orderDetails[i][item.tableName]!=undefined) {

                    if(orderTaskId == ''){
                       var newList = orderDetails[i]["ORDER_TASK"].filter((o) =>{return o.taskId == temp.taskId});
                       newList.forEach(m => {
                            // 
                            orderTaskId = m.orderTaskId;
                        });
                    }


                    var newList = orderDetails[i][item.tableName].filter(o => {return o.attrId == item.colId && o.orderTaskId == orderTaskId});
                    // 
                    newList.forEach(m => {
                            // 
                            // 
                            item.value = m.attrVal;
                            item.oldValue=m.attrVal; 
                            item.instId=m['orderTaskAttrId'];
                    });
                }
                else if (item.tableName == 'ORDER_CONTACT_INFO') {
                    orderDetails[i][item.tableName].forEach(m => {
                        if (m.contactType == "6" && item.attrRoleCd == "100") {
                            item.value = pageOfferDetail.getVal(m[item.colId]);
                            item.oldValue= pageOfferDetail.getVal(m[item.colId]);
                            item.instId=m['orderContactInfoId'];
                        }
                        if (m.contactType == "7" && item.attrRoleCd == "200") {
                            item.value = pageOfferDetail.getVal(m[item.colId]);
                            item.oldValue= pageOfferDetail.getVal(m[item.colId]);
                            item.instId=m['orderContactInfoId'];
                        }
                        if (m.contactType == "1" && (item.attrRoleCd == "0"||!item.attrRoleCd)) {
                            item.value = pageOfferDetail.getVal(m[item.colId]);
                            item.oldValue= pageOfferDetail.getVal(m[item.colId]);
                            item.instId=m['orderContactInfoId'];
                        }
                        
                    });
                }
                else if (item.tableName == 'ORDER_ITEM_FILE') {
                    orderDetails[i][item.tableName].forEach(m => {
                        item.value = pageOfferDetail.getVal(m[item.colId]);
                        item.oldValue= pageOfferDetail.getVal(m[item.colId]);
                        item.instId=m['custOrderFileId'];
                    });
                }
                else if (item.tableName == 'ACCT_ITEM') {
                    orderDetails[i][item.tableName].forEach(m => {
                        
                        if (m.acctTypeCd== item.attrRoleCd) {
                            if(item.colId == 'amount'){
                                if(item.acctItemId==m.acctItemTypeId){
                                    item.value = pageOfferDetail.getVal(m.amount);
                                    item.oldValue= pageOfferDetail.getVal(m.amount);
                                    item.instId=m['acctItemId'];   
                                }
                                pageOfferDetail.setAmoutRelattr(item,orderDetails[i],objArr);
                            }else{
                                item.value = pageOfferDetail.getVal(m[item.colId]);
                                item.oldValue= pageOfferDetail.getVal(m[item.colId]);
                                item.instId=m['acctItemId'];   
                            }
                            
                        }
                        if ((m.acctTypeCd == 0) && !item.attrRoleCd) {
                            if(item.colId == 'amount'){
                                if(item.acctItemId==m.acctItemTypeId){
                                    item.value = pageOfferDetail.getVal(m.amount);
                                    item.oldValue= pageOfferDetail.getVal(m.amount);
                                    item.instId=m['acctItemId'];
                                }
                                pageOfferDetail.setAmoutRelattr(item,orderDetails[i],objArr);
                            }else{
                                item.value = pageOfferDetail.getVal(m[item.colId]);
                                item.oldValue= pageOfferDetail.getVal(m[item.colId]);
                                item.instId=m['acctItemId'];
                            }     
                        }
                        if(!item.attrRoleCd){    
                            item.value = pageOfferDetail.getVal(m[item.colId]);
                            item.oldValue= pageOfferDetail.getVal(m[item.colId]); 
                            item.instId=m['acctItemId'];
                            if(item.instIds){
                                let instIds=item.instIds.split(";");
                                if(!instIds.includes(""+item.instId)){
                                    item.instIds=item.instIds+";"+item.instId;
                                }
                            }else{
                                item.instIds=""+item.instId;
                            }
                        }
                    });
                }
                else if (item.tableName == 'ACCT_ITEM_ORG') {
                    orderDetails[i][item.tableName].forEach(m => {
                        if (m.chargeOrgCd== item.attrRoleCd) {
                            if(item.text !=undefined && m[item.colId]!=''){
                                item.value = pageOfferDetail.getVal(m[item.colId]);
                                item.oldValue= pageOfferDetail.getVal(m[item.colId]);
                                let colName=item.colId.replace('Code', 'Name');
                                item.text = m[colName];
                            }else{
                                item.value = pageOfferDetail.getVal(m[item.colId]);
                                item.oldValue= pageOfferDetail.getVal(m[item.colId]);
                            }
                            item.instId=m['acctItemOrgId'];                           
                        }
                        else if ((m.chargeOrgCd == 0) && !item.attrRoleCd) {
                           item.value = pageOfferDetail.getVal(m[item.colId]);
                           item.oldValue= pageOfferDetail.getVal(m[item.colId]);
                           item.instId=m['acctItemOrgId'];  
                        }
                        if(!item.attrRoleCd){
                            item.value =''+ pageOfferDetail.getVal(m[item.colId]);
                            item.oldValue=''+ pageOfferDetail.getVal(m[item.colId]);
                            item.instId=m['acctItemOrgId'];
                            if(item.instIds){
                                let instIds=item.instIds.split(";");
                                if(!instIds.includes(""+item.instId)){
                                    item.instIds=item.instIds+";"+item.instId;
                                }
                            }else{
                                item.instIds=""+item.instId;
                            }
                        }

                    });
                }
                else if (item.tableName == 'ORD_OFFER_INST_FEE_INFO') {
                    orderDetails[i][item.tableName].forEach(m => {
                        if (m.roleCd == item.attrRoleCd) {
                            if(item.colId == 'amount'){
                                if(item.acctItemId==m.acctItemTypeId){
                                    item.value = pageOfferDetail.getVal(m.amount);
                                    item.oldValue= pageOfferDetail.getVal(m.amount);
                                }
                                pageOfferDetail.setAmoutRelattr(item,orderDetails[i],objArr);
                            }else{
                                item.value = pageOfferDetail.getVal(m[item.colId]);
                                item.oldValue= pageOfferDetail.getVal(m[item.colId]);
                                
                            }
                            item.instId=m['offerInstFeeInfoId'];
                            item.rowId=m['rowId'];
                        }
                        else if ((m.roleCd == 0) && !item.attrRoleCd) {
                            if(item.colId == 'amount'){
                                if(item.acctItemId==m.acctItemTypeId){
                                    item.value = pageOfferDetail.getVal(m.amount);
                                    item.oldValue= pageOfferDetail.getVal(m.amount);
                                    item.instId=m['offerInstFeeInfoId'];
                                    item.rowId=m['rowId'];
                                }
                                pageOfferDetail.setAmoutRelattr(item,orderDetails[i],objArr);
                            }          
                        }
                        // if(!item.attrRoleCd){    
                        //     item.value = m[item.colId];                                                    
                        // }
                    });
                }
                if (item.tableName == 'ORD_PROD_INST_ACCT_REL') {
                    orderDetails[i][item.tableName].forEach(m => {                   
                        if(!item.attrRoleCd&&item.colId=='acctId'&& m['acctName']!=undefined){    
                            item.value = pageOfferDetail.getVal(m['acctName']);
                            item.oldValue= pageOfferDetail.getVal(m['acctName']);
                            item.instId=m['prodInstAcctRelId']; 
                            let rowId=m['rowId'];
                            if(rowId!=''&&rowId!=undefined){
                                item.rowId=rowId
                               if(item.rowIds){
                                    let rowIds=item.rowIds.split(";");
                                    if(!rowIds.includes(""+rowId)){
                                        item.rowIds=item.rowIds+";"+rowId;
                                    }
                         
                                }else{
                                    item.rowIds=""+rowId;
                                }
                            }                                        
                        }
                    });
                }

                else if (item.tableName == 'ORD_OFFER_INST_FEE_ATTR') {
                    orderDetails[i][item.tableName].forEach(m => {
                        if (m.attrId == item.colId) {
                            if(item.text!=undefined){
                                item.value =m.attrValue;  
                                item.oldValue=m.attrValue; 
                                item.text = m['attrValueName'];
                            }else{
                                item.value = m.attrValue;
                                item.oldValue=m.attrValue;
                            }
                            item.instId=m['offerInstFeeAttrId'];
                            item.rowId=m['rowId'];

                        }
                    });
                }


            }
        }
        // 
        return objArr;
    },
    /**
     * 获取主产品订单项
     * @param {*} orderItems 
     * @param {*} mainProdId 
     * @returns 
     */
    getOrderItemByMainProdId:(orderItems,mainProdId)=>{
       let orderItem;
       orderItems.forEach(i=>{
           if(i.applyObjSpec==mainProdId){
            orderItem=i;
            return;
           }
       })
       return orderItem;
    },
    /**
     * 暂时不用
     * @param {*} prodAttrAll 
     * @param {*} prodInstId 
     * @returns 
     */
    getProdAttrByProdInstId:(prodAttrAll,prodInstId)=>{
        let prodAttrs=[];
        prodAttrAll.forEach(a=>{
            if(a.prodInstId==prodInstId){
                prodAttrs.push(a);
            }
        })
        return prodAttrs;

    },
    /**
     * 暂时不用
     * @param {*} res 
     * @param {*} RoleCd 
     * @param {*} data 
     * @param {*} tableName 
     * @param {*} mountTableName 
     */
    setInstAcctRel: (res, RoleCd,data,tableName,mountTableName) => {
        let rowData=pageOfferDetail.getDataByRoleCd(data[tableName],RoleCd);
        if(rowData){
            let intAcctAttr = res.filter((item) => {
                return item.tableName == tableName && item.colId !== 'amount'
            })
    
            let newMount = res.filter((item) => {
                return item.tableName == mountTableName && item.colId == 'amount'
            })
    
            newMount.forEach((item) => {            
                let mapArr = [];
                // 
                if (item.attrRoleCd == RoleCd) {
                    if(item.relIds){
                        mapArr = item.relIds.split(";")
                    }
                    mapArr.forEach((mapAttrItem)=>{
                        intAcctAttr.forEach((acctAttrItem)=>{
                            let id=acctAttrItem.id;
                            if(mapAttrItem.includes(id)){
                                acctAttrItem.value= rowData[acctAttrItem.colId];
                            }
    
                        })
                    })
                }
            })
        }
        
    },
    /**
     * 根据rolecd获取实例数据
     * @param {*} data 
     * @param {*} RoleCd 
     * @returns 
     */
    getDataByRoleCd:(data,RoleCd)=>{
        let rowData;
        data.forEach(i=>{
            if(i.roleCd==RoleCd){
               rowData=i;
               return;
            }
        })
        return rowData;
    },
    /**
     * 去除未定义字段属性
     * @param {*} val 
     * @returns 
     */
    getVal:(val)=>{
        if(val!=undefined){
            return val;
        }else{
            return '';
        }
    },
    /**
     * 资费信息根据relIds设置关联属性信息
     * @param {*} amoutItem 
     * @param {*} orderDetail 
     * @param {*} objArr 
     */
    setAmoutRelattr:(amoutItem,orderDetail, objArr)=>{
      let relIds=amoutItem.relIds;
      if(relIds!=undefined&&relIds!=""){
        let mapArr = relIds.split(";")
        mapArr.forEach(attr=>{
            for (let key in objArr) {
                let item = objArr[key];
                if(attr==item.id){
                    orderDetail[item.tableName].forEach(m => {
                        if((amoutItem.acctItemId==m.acctItemTypeId||amoutItem.acctItemId==m.acctItemGroupId)&&m[item.colId]!=undefined){
                            if(item.attrRoleCd==m.roleCd){
                                if(item.text !=undefined){
                                    item.value = pageOfferDetail.getVal(m[item.colId]);
                                    item.oldValue= pageOfferDetail.getVal(m[item.colId]);
                                    let colName=item.colId.replace('Code', 'Name');
                                    item.text = pageOfferDetail.getVal(m[colName]);
                                }else{
                                    item.value = pageOfferDetail.getVal(m[item.colId]);
                                    item.oldValue= pageOfferDetail.getVal(m[item.colId]);
                                }
                                if(item.tableName=='ACCT_ITEM'){
                                    item.instId=m['acctItemId'];
                                }else if(item.tableName=='ORD_OFFER_INST_FEE_INFO'){
                                    item.instId=m['offerInstFeeInfoId'];
                                    item.rowId=m['rowId'];
                                }else if(item.tableName=='ACCT_ITEM_ORG'){
                                    item.instId=m['acctItemOrgId'];
                                }else if(item.tableName=='ORD_PROD_INST_ACCT_REL'){
                                    item.instId=m['prodInstAcctRelId'];
                                    item.rowId=m['rowId'];
                                }
                            } else if(!item.attrRoleCd) {
                                
                                if(item.text !=undefined){
                                    item.value = m[item.colId];
                                    item.oldValue=m[item.colId];
                                    let colName=item.colId.replace('Code', 'Name');
                                    item.text = m[colName];
                                }else{
                                    item.value = m[item.colId];
                                    item.oldValue=m[item.colId];
                                }
                                let instId;
                                let rowId='';
                                if(item.tableName=='ACCT_ITEM'){
                                    instId=m['acctItemId'];
                                }else if(item.tableName=='ORD_OFFER_INST_FEE_INFO'){
                                    instId=m['offerInstFeeInfoId'];
                                    rowId=m['rowId'];
                                }else if(item.tableName=='ACCT_ITEM_ORG'){
                                    instId=m['acctItemOrgId'];
                                }else if(item.tableName=='ORD_PROD_INST_ACCT_REL'){
                                    instId=m['prodInstAcctRelId'];
                                    rowId=m['rowId'];
                                }
                                item.instId=instId
                                if(item.instIds){
                                    let instIds=item.instIds.split(";");
                                    if(!instIds.includes(""+instId)){
                                        item.instIds=item.instIds+";"+instId;
                                    }
                             
                                }else{
                                    item.instIds=""+instId;
                                }

                                if(rowId!=''&&rowId!=undefined){
                                    item.rowId=rowId
                                   if(item.rowIds){
                                        let rowIds=item.rowIds.split(";");
                                        if(!rowIds.includes(""+rowId)){
                                            item.rowIds=item.rowIds+";"+rowId;
                                        }
                             
                                    }else{
                                        item.rowIds=""+rowId;
                                    }
                                }
                                
                                // idMap.set(instId,instId);
                            }                       
                           

    

                        }
                    })
                }
            }
        })

      }
    }



};

export default pageOfferDetail;
